#!/usr/bin/perl -w
##############################################################################
#
# test_Report.pl
# ==============
#
# Author: Steven Liang <stliang@gmail.com>
#
# Purpose:
#   test_Report demonstrates reporting test result to local file system.
#   Since the result summary files are in html format, it is intended these
#   results are web accessible.
#
# Assumptions:
#   1.) The PAT_REPORT environment variable is set
#   2.) A virtual link is created to link the /var/www/html/PAT/ 
#       to $PAT_REPORT directory
#
# Steps:
#
#   1.) Instantiate a CiscoRouter object and call it C2600
#   2.) Start a telnet session to target host
#   3.) Log the entire telnet session to a file
#   4.) Login to the target host
#   5.) Execute Cisco 2600 router's "show ip route" command
#   6.) Parse the command output and extra out the default route info
#   7.) Release C2600's resource
#
# Expected result:
#
#   Telnet to cisco router and perform the show ip route command and exit cleanly.
#
##############################################################################

package main;
$VERSION = 1.01;

use AutoTest::Exceptions;
use ReportTo;
use Setup_B;
use BaseRC;
use TestReportRC;

# Set script variables
my $MySetup = undef;
my $MyReportTo = undef;

my $actualGateWay = "";

# Try block
eval {
  # Check if environment variables are set
  if (!defined $ENV{'PAT_ROOT'}) {
	AutoTest::Exception::Environment->throw('PAT_ROOT environment variable not set');
  }
  if (!defined $ENV{'PAT_LOG'}) {
	AutoTest::Exception::Environment->throw('PAT_LOG environment variable not set');
  }
  if (!defined $ENV{'PERLLIB'}) {
	AutoTest::Exception::Environment->throw('PERLLIB environment variable not set');
  }

  # Instantiate a test Setup_B object and call it MySetup
  $MySetup = Setup_B->new();

  # Instantiate a ReportTo object and call it MyReportTo
  $MyReportTo = ReportTo->new($BaseRC::release, $BaseRC::build);

  # Set the total number of test cases to be 1
  $MyReportTo->{Local}->total(1);

  # Start a telnet session to the C2600 cisco router installed in Setup_B
  $MySetup->{C2600}->openTelnet();

  # Log the entire telnet session to a file
  $MySetup->{C2600}->log($ENV{'PAT_LOG'} . $TestReportRC::logFileName);

  $MySetup->{C2600}->logMessage("\nLogin to the target host\n");
  $MySetup->{C2600}->login(undef, qr/Password: $/, qr/>/, qr/#/);

  $MySetup->{C2600}->logMessage("\nExecute Cisco 2600 router show ip route command\n");
  $myRoute = $MySetup->{C2600}->showIPRoute();

  $MySetup->{C2600}->logMessage("\nParse the command output and extra out the default route info\n");
  @lines = split ( /\n/, $myRoute);

  foreach $line (@lines) {
	if ($line =~ /S\*/) {
	    $line =~ m{.*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).* (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})};
	    # $1 holds the first match and $2 holds the second match
	    $MySetup->{C2600}->logMessage("\nDefault distination network: $1\n");
	    $MySetup->{C2600}->logMessage("\nDefault gateway: $2\n");
	    $actualGateWay = $2 if defined($2);
	    last;
	}
  }

  # Stop logging telnet session output
  $MySetup->{C2600}->stopLog();

  # Release test setup resouce
  $MySetup->{C2600}->DESTROY();

  # Report test result
  if ($TestReportRC::expectedGateWay eq $actualGateWay) {
	$MyReportTo->{Local}->appendTestReport("1.) PASS: Expected default gateway found");
	$MyReportTo->{Local}->incrPass();
	$MyReportTo->{Local}->saveTestReport($TestReportRC::scriptName);
  } else {
	$MyReportTo->{Local}->appendTestReport("1.) FAIL: Expected default gateway not found");
	$MyReportTo->{Local}->incrFail();
	$MyReportTo->{Local}->saveTestReport($TestReportRC::scriptName);
  }
  
  # Generate test report summary
  $MyReportTo->{Local}->genSummary();
};

# Catch block
if ($@) {
  if ( !UNIVERSAL::isa( $@, 'AutoTest::Exception::Connection' ) ) {
  	# Stop logging telnet session output
  	$MySetup->{C2600}->stopLog();
	
	# Release test setup resouce
	$MySetup->{C2600}->DESTROY();
  }
  if ( UNIVERSAL::isa( $@, 'AutoTest::Exception' ) ) {

	# Save what ever test result avaliable and generate a summary
	$MyReportTo->{Local}->appendTestReport($@->error);
	$MyReportTo->{Local}->appendTestReport($@->trace->as_string);
	$MyReportTo->{Local}->saveTestReport($TestReportRC::scriptName);
	$MyReportTo->{Local}->genSummary();

  	warn $@->error, "\n", $@->trace->as_string, "\n";
  	warn join ' ',  $@->package, $@->euid, $@->egid, $@->uid, $@->gid, $@->pid, $@->time;

  } else {
	# Not an error from AutoTest::Exception
	ref $@ ? $@->rethrow : die $@;
  }
}
